//
//  CSResultController.m
//  CreateWithSuper
//
//  Created by chenjunbo on 2024/6/30.
//  Copyright © 2024 CS. All rights reserved.
//

#import "CSResultController.h"
#import "SDWebImage/SDWebImage.h"
#import <GKPhotoBrowser/GKPhotoBrowser.h>

#import "CSLiabilityPopView.h"
#import "CSReportController.h"
#import <Photos/Photos.h>
#import "CSLimitedController.h"
#import "ADModule.h"
#import "CSNetworkHelper.h"
#import "CSCreatingVC.h"

@interface CSResultController ()

@property (weak, nonatomic) IBOutlet UIImageView *resultImgView;
@property (weak, nonatomic) IBOutlet UILabel *createLabel;

@property(nonatomic,strong)PHAssetCollection *createCollection;// 相册
@end

@implementation CSResultController

- (void)viewDidLoad {
    [super viewDidLoad];
        
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.createLabel.text = self.createStr;
        SDWebImageOptions options = SDWebImageProgressiveLoad | SDWebImageHighPriority | SDWebImageFromLoaderOnly;

        [self.resultImgView sd_setImageWithURL:[NSURL URLWithString:self.resultImgStr] placeholderImage:nil options:options];
    });
    
    self.resultImgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(gesClick)];
    [self.resultImgView addGestureRecognizer:ges];
    
    id traget = self.navigationController.interactivePopGestureRecognizer.delegate;
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
    [self.view addGestureRecognizer:pan];
}

- (void)gesClick{
    
    NSMutableArray *photos = [NSMutableArray new];
    
    GKPhoto *photo = [GKPhoto new];

    photo.url = [NSURL URLWithString:self.resultImgStr];
    photo.sourceImageView = self.resultImgView;
    
    [photos addObject:photo];
    
    GKPhotoBrowser *browser = [[GKPhotoBrowser alloc] initWithPhotos:photos currentIndex:0];
    [browser showFromVC:self];
}

- (IBAction)backClick:(UIButton *)sender {
    if (self.isLimited){
        [self poptoLimited];
    }else{
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

- (IBAction)tipClick:(UIButton *)sender {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Create" bundle:nil];
            CSLiabilityPopView *popView = [storyboard instantiateViewControllerWithIdentifier:@"CSLiabilityPopView"];
            
            popView.modalPresentationStyle = UIModalPresentationOverFullScreen;
            [self presentViewController:popView animated:YES completion:nil];
    
            UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
            view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
            [[UIApplication sharedApplication].keyWindow addSubview:view];
    
            popView.okBlock = ^{
                [view removeFromSuperview];
            };


        });
}

- (IBAction)saveClick:(UIButton *)sender {
    UIImageWriteToSavedPhotosAlbum(self.resultImgView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    
    NSString *msg = nil ;
    if(error){
        
        NSURL *url1 = [NSURL URLWithString:@"App-Prefs:root=Photos"];
        NSURL *url2 = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if (@available(iOS 11.0, *)) {
            if ([[UIApplication sharedApplication] canOpenURL:url2]){
                [[UIApplication sharedApplication] openURL:url2 options:@{} completionHandler:nil];
            }
        } else {
            if ([[UIApplication sharedApplication] canOpenURL:url1]){
                if (@available(iOS 10.0, *)) {
                    [[UIApplication sharedApplication] openURL:url1 options:@{} completionHandler:nil];
                } else {
                    [[UIApplication sharedApplication] openURL:url1];
                }
            }
        }

    }else{
        
        [[ADModule standardInstance]showInterAdWithVC:self dismiss:^{
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Create" bundle:nil];
                CSReportController *vc = [storyboard instantiateViewControllerWithIdentifier:@"CSReportController"];
                vc.shareImage = self.resultImgView.image;
                [self.navigationController pushViewController:vc animated:YES];

            });
            

        }];

    }
}

- (void)poptoLimited{
    UINavigationController *navController = (UINavigationController *)self.navigationController;
    NSArray *childViewControllers = navController.childViewControllers;
    for (UIViewController *childVC in childViewControllers) {
        for (UIViewController *childVC in childViewControllers) {
            if ([childVC isKindOfClass:[CSLimitedController class]]) {
                [self.navigationController popToViewController:childVC animated:YES];
                break;
            }
        }
    }
}

- (IBAction)deleteClick:(UIButton *)sender {
    if (self.isLimited){
        [self poptoLimited];
    }else{
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

- (IBAction)reClick:(UIButton *)sender {
    
    if (self.isLocal){
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Create" bundle:nil];
        CSCreatingVC *vc = [storyboard instantiateViewControllerWithIdentifier:@"CSCreatingVC"];
        vc.taskID = @"";
        vc.createStr = self.createStr;
        vc.isLocal = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        [[CSNetworkHelper standardInstance]requestTxt2img:self.createStr callback:^(NSString * _Nonnull result) {

            if (result.length > 0){
                UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Create" bundle:nil];
                CSCreatingVC *vc = [storyboard instantiateViewControllerWithIdentifier:@"CSCreatingVC"];
                vc.taskID = result;
                vc.createStr = self.createStr;
                [self.navigationController pushViewController:vc animated:YES];
            }
            
        }];
    }
    


}


    
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
